/* JavaLog - package tracker 
 * 
 * Copyright (C) 2009  Allyson Lima, Diego Pedro, Victor Freire
 * 
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software 
 * Foundation, either version 3 of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE.  See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with this 
 * program.  If not, see <http://www.gnu.org/licenses/>.
 */

package br.edu.ufcg.ccc.javalog.view;
import java.awt.Component;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.LayoutStyle;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/

/** 
 * Main window.
 * @author Allyson Lima, Diego Pedro, Victor Freire
 * @version 22/11/09 
 */
@SuppressWarnings("serial") // not serializing this...
public class MainView extends AbstractView {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JLabel labelProgramName;
	private JButton buttonExit;
	private AbstractAction actionManagePackages;
	private JButton buttonManagePackages;
	private AbstractAction actionManageFranchises;
	private AbstractAction actionExitProgram;
	private JButton buttonManageFranchises;

	static 
	{
		JFrame.setDefaultLookAndFeelDecorated(true);
		JDialog.setDefaultLookAndFeelDecorated(true);
	}
	
	/**
	* Auto-generated main method to display this JFrame
	*/
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				MainView inst = new MainView();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}
	
	public MainView() {
		super();
		initGUI();
	}
	
	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			GroupLayout thisLayout = new GroupLayout((JComponent)getContentPane());
			getContentPane().setLayout(thisLayout);
			this.setTitle("JavaLog");
			{
				labelProgramName = new JLabel();
				labelProgramName.setText("JavaLog - Rastreador de Encomendas");
				labelProgramName.setHorizontalAlignment(SwingConstants.CENTER);
			}
			{
				buttonManageFranchises = new JButton();
				buttonManageFranchises.setText("Gerenciar Franquias");
				buttonManageFranchises.setMnemonic(java.awt.event.KeyEvent.VK_F);
				buttonManageFranchises.setAction(getActionManageFranchises());
			}
			{
				buttonExit = new JButton();
				buttonExit.setText("Sair");
				buttonExit.setAction(getActionExitProgram());
				buttonExit.setMnemonic(java.awt.event.KeyEvent.VK_S);
			}
			thisLayout.setVerticalGroup(thisLayout.createSequentialGroup()
				.addContainerGap()
				.addComponent(labelProgramName, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
				.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
				.addComponent(buttonManageFranchises, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
				.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED, 1, GroupLayout.PREFERRED_SIZE)
				.addComponent(getButtonManagePackages(), GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
				.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED, 1, GroupLayout.PREFERRED_SIZE)
				.addComponent(buttonExit, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
				.addContainerGap(36, Short.MAX_VALUE));
			thisLayout.setHorizontalGroup(thisLayout.createSequentialGroup()
				.addContainerGap(41, 41)
				.addGroup(thisLayout.createParallelGroup()
				    .addComponent(labelProgramName, GroupLayout.Alignment.LEADING, 0, 313, Short.MAX_VALUE)
				    .addComponent(buttonManageFranchises, GroupLayout.Alignment.LEADING, 0, 313, Short.MAX_VALUE)
				    .addComponent(buttonExit, GroupLayout.Alignment.LEADING, 0, 313, Short.MAX_VALUE)
				    .addComponent(getButtonManagePackages(), GroupLayout.Alignment.LEADING, 0, 313, Short.MAX_VALUE))
				.addContainerGap(40, 40));
			thisLayout.linkSize(SwingConstants.VERTICAL, new Component[] {buttonManageFranchises, buttonExit});
			pack();
			this.setSize(402, 193);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * Shuts down the application.
	 */
	private void exitProgram() {
		System.exit(0);
	}
	
	/**
	 * Returns the action for the Exit button.
	 */
	private AbstractAction getActionExitProgram() {
		if(actionExitProgram == null) {
			actionExitProgram = new AbstractAction("Sair", null) {
				public void actionPerformed(ActionEvent evt) {
					exitProgram();
				}
			};
		}
		return actionExitProgram;
	}
	/**
	* This method should return an instance of this class which does 
	* NOT initialize it's GUI elements. This method is ONLY required by
	* Jigloo if the superclass of this class is abstract or non-public. It 
	* is not needed in any other situation.
	 */
	public static Object getGUIBuilderInstance() {
		return new MainView(Boolean.FALSE);
	}
	
	/**
	 * This constructor is used by the getGUIBuilderInstance method to
	 * provide an instance of this class which has not had it's GUI elements
	 * initialized (ie, initGUI is not called in this constructor).
	 */
	public MainView(Boolean initGUI) {
		super();
	}
	
	private AbstractAction getActionManageFranchises() {
		if(actionManageFranchises == null) {
			actionManageFranchises = new AbstractAction("Gerenciar Franquias", null) {
				public void actionPerformed(ActionEvent evt) {
					WindowManager.getInstance().openNewWindow(new FranchiseManagerView());
				}
			};
			actionManageFranchises.putValue(javax.swing.Action.MNEMONIC_KEY, new Integer(java.awt.event.KeyEvent.VK_F));
		}
		return actionManageFranchises;
	}
	
	private JButton getButtonManagePackages() {
		if(buttonManagePackages == null) {
			buttonManagePackages = new JButton();
			buttonManagePackages.setText("Gerenciar Pacotes");
			buttonManagePackages.setAction(getActionManagePackages());
		}
		return buttonManagePackages;
	}
	
	private AbstractAction getActionManagePackages() {
		if(actionManagePackages == null) {
			actionManagePackages = new AbstractAction("Gerenciar Pacotes", null) {
				public void actionPerformed(ActionEvent evt) {
					WindowManager.getInstance().openNewWindow(new PackageManagerView());
				}
			};
			actionManagePackages.putValue(javax.swing.Action.MNEMONIC_KEY, new Integer(java.awt.event.KeyEvent.VK_P));
		}
		return actionManagePackages;
	}

}
